//
//  WayPointImageView.m
//  MobileFaviBook
//
//  Created by easystudio on 9/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "InfoImageView.h"
#import "ImageLoader.h"
#import "HttpServerURL.h"
#import "Type.h"

@implementation InfoImageView
@synthesize _uiImage;
@synthesize _uiTitle;
@synthesize _uiLoading;
@synthesize _uiDescripation;
@synthesize _sImageURL;
@synthesize _uiLabel;
@synthesize _sDes;
@synthesize _sTitle;
@synthesize _image;

@synthesize imageParams;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    self._uiImage = nil;
    self._uiTitle = nil;
    self._uiLoading = nil;
    self._uiDescripation = nil;
    self._sImageURL = nil;
    self._uiLabel = nil;
    self._sDes = nil;
    self._sTitle = nil;
    self._image = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self.view setBackgroundColor:BACKGROUND_COLOR];

    self.view.userInteractionEnabled=YES;
    
    [self refreshimage];
    
//    NSURL *url=[NSURL URLWithString:_sImageURL];
//    NSData *data=[NSData dataWithContentsOfURL:url];
//    UIImage *tmpimage=[[UIImage alloc] initWithData:data];
//    _uiImage.image=tmpimage;
//    [_uiLoading setHidden:YES];
    
    // Do any additional setup after loading the view from its nib.
}

-(void)refreshimage
{
    if (_sImageURL) {
        [[ImageLoader sharedLoader] setImageWithURL:_sImageURL  toView:_uiImage];
        [_uiLoading startAnimating];
        _uiTitle.text = _sTitle;
        _uiDescripation.text = _sTitle;
    }
    else
    {
        _uiTitle.text = _sTitle;
        _uiDescripation.text = _sDes;
        [_uiLoading setHidden:YES];
        if (_image == nil) {
            _uiLabel.text = @"没有照片";
        }        
        _uiImage.image = _image;
    }

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    lastLoction = location;
    moveChanged = NO;
    moveflag = NO;
}



-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    if(!moveChanged &&  fabs(location.x -  lastLoction.x) >  60 && fabs(location.y - lastLoction.y) < 25){
        moveChanged = YES;
        if(location.x -  lastLoction.x >0){///往右 上一张图片
//            NSLog(@"this right");
            if (show_num > 0) {
                show_num--;
                NSMutableDictionary *tmp_dictionary = [[NSMutableDictionary alloc] initWithDictionary:[imageParams objectAtIndex:show_num]];
                NSString *img_url=[[NSString alloc]initWithFormat:@"%@%@",[HttpServerURL GetInstance].serverImgUrl,[tmp_dictionary objectForKey:@"id"]];
                self._sImageURL = img_url;
                self._sTitle = [tmp_dictionary objectForKey:@"file_name"];;
                self._sDes = @"";
                
                [img_url release];
                [tmp_dictionary release];
                [self refreshimage];
            }

            //[self switchViews:nil];
        }else{//往左 下一张图片
//            NSLog(@"this left");
            if (show_num < ([imageParams count]-1)) {
                show_num++;
                NSMutableDictionary *tmp_dictionary = [[NSMutableDictionary alloc] initWithDictionary:[imageParams objectAtIndex:show_num]];
                NSString *img_url=[[NSString alloc]initWithFormat:@"%@%@",[HttpServerURL GetInstance].serverImgUrl,[tmp_dictionary objectForKey:@"id"]];
                self._sImageURL = img_url;
                self._sTitle = [tmp_dictionary objectForKey:@"file_name"];;
                self._sDes = @"";
                
                [img_url release];
                [tmp_dictionary release];
                [self refreshimage];
            }
        }
    }
    moveflag = YES;
        
}



-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location = [[touches anyObject] locationInView:self.view];
    lastLoction = location;
    if (moveflag == NO) {
//        NSLog(@"single click");
        [self close];
    }
    moveflag = NO;
}

-(void) setShowNum:(int)param
{
    show_num = param;
}

-(void) close
{
    [UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1];
	[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES];

    [self viewWillDisappear:YES];
    [self.view removeFromSuperview];
    [self viewDidDisappear:YES];
    
    [UIView commitAnimations];
}
@end
